package com.data.center.config;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @descripition:默认的时间格式配置类
 * */
public class CustomDateTypeHandler implements TypeHandler<Date> {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        preparedStatement.setDate(i, date);
    }

    @Override
    public Date getResult(ResultSet resultSet, String s) throws SQLException {
        java.util.Date utilDate = resultSet.getDate(s);
        return new Date(utilDate.getTime());
    }

    @Override
    public Date getResult(ResultSet resultSet, int i) throws SQLException {
        java.util.Date utilDate = resultSet.getDate(i);
        return new Date(utilDate.getTime());
    }

    @Override
    public Date getResult(CallableStatement callableStatement, int i) throws SQLException {
        java.util.Date utilDate = callableStatement.getDate(i);
        return new Date(utilDate.getTime());
    }
}
